<template>
	<view class="container">
		<view class="search">
			<MySearch @click="search"></MySearch>
		</view>
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="item in list" :key="item.goods_id">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?id=${item.goods_id}`">
					<image :src="item.image_src" style="width: 100%;"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 导航栏 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navClick(item)">
				<image :src="item.image_src" mode="widthFix"></image>
			</view>
		</view>

		<!-- 楼层 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, i) in floorList" :key="i">
				<image :src="item.floor_title.image_src" mode="" class="floor-title"></image>
				<view class="floor-contain">
					<view class="floor-contain-left">
						<image :src="item.product_list[0].image_src" mode="widthFix"
							@click="floorimgClick(item.product_list[0].url)"></image>
					</view>
					<view class="floor-contain-right">
						<view class="right-img">
							<image @click="floorimgClick(item2.url)" :src="item2.image_src" mode="widthFix" v-if="i2!==0"
								v-for="(item2, i2) in item.product_list" :key="i2"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import badge from '../../mixins/cart-badge.js'
	import MySearch from '../../components/my-search/my-search.vue'
	import {
		getSwiperList,
		getNavList,
		getFloorList
	} from '../../api/index.js'
	export default {
		mixins: [badge],
		data() {
			return {
				list: [], // 轮播图列表
				navList: [],
				floorList: []
			}
		},
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		components: {
			MySearch
		},
		methods: {
			// 获取轮播图数据
			async getSwiperList() {
				try {
					const res = await getSwiperList()
					this.list = res.message
				} catch (e) {
					uni.$showMsg('数据加载失败')
				}
			},
			// 获取nav数据
			async getNavList() {
				try {
					const res = await getNavList()
					this.navList = res.message
				} catch (e) {
					uni.$showMsg('数据加载失败')
				}
			},
			navClick(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			// 获取楼层数据
			async getFloorList() {
				try {
					const res = await getFloorList()
					res.message.forEach(item => {
						item.product_list.forEach(item2 => {
							item2.url = '/subpkg/goods_list/goods_list?' + item2.navigator_url.split(
								'?')[1]
						})
					})
					this.floorList = res.message
					console.log(this.floorList)
				} catch (e) {
					uni.$showMsg('获取数据失败')
				}
			},
			floorimgClick(url) {
				uni.navigateTo({
					url: url
				})
			},
			search() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		},

	}
</script>

<style lang="scss" scoped>
	.search {
		position: sticky;
		top: 0;
		width: 100%;
		z-index: 999;
	}

	swiper {
		image {
			height: 330rpx;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15rpx 0;

		.nav-item {
			image {
				width: 120rpx;
			}
		}
	}

	.floor-list {
		.floor-item {
			margin-bottom: 10rpx;
			.floor-title {
				width: 100%;
				height: 60rpx;
			}

			.floor-contain {
				display: flex;
				padding-left: 14rpx;

				.floor-contain-left {
					width: 232rpx;
					image {
						width: 232rpx;
					}
				}

				.floor-contain-right {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;

					.right-img {
						image {
							width: 232rpx;
						}
					}
				}
			}
		}
	}
</style>
